import java.util.*;
public class Test16k {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Queue<String> queue = new LinkedList<>();
        int count = sc.nextInt();
        int n = 0;
        while(n < count){
            String num = sc.next();
            queue.add(num);
            n++;
        }
        while (!queue.isEmpty()) {
            String num = queue.poll();
            Integer num1 = Integer.parseInt(num,16);
            System.out.println(Integer.toOctalString(num1));
        }
    }
}
